|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
var expect = require('expect.js'); |
|
4
|
|
|
|
|
5
|
|
|
var Position = require('../lib/position'); |
|
6
|
|
|
var Grid = require('../lib/grid'); |
|
7
|
|
|
var Mower = require('../lib/mower'); |
|
8
|
|
|
|
|
9
|
|
|
describe('Mower', function() { |
|
10
|
|
|
describe('start', function () { |
|
11
|
|
|
var grid = new Grid(new Position({x:5,y:5}, 'N')); |
|
12
|
|
|
it('should stop at positon 1 3 N', function (done) { |
|
13
|
|
|
var position = new Position({x:1,y:2}, 'N'), |
|
14
|
|
|
mower = new Mower(0, grid, position, ['G','A','G','A','G','A','G','A','A']); |
|
15
|
|
|
mower.start().then(function(mowerUpdated) { |
|
16
|
|
|
expect(mowerUpdated.position).to.eql({ x:1, y:3, c:'N' }); |
|
17
|
|
|
done(); |
|
18
|
|
|
}); |
|
19
|
|
|
}); |
|
20
|
|
|
it('should stop at positon 4 1 E', function (done) { |
|
21
|
|
|
var position = new Position({x:3,y:3}, 'E'), |
|
22
|
|
|
mower = new Mower(0, grid, position, ['A','D','A','A','D','A','D','D','A']); |
|
23
|
|
|
mower.start().then(function(mowerUpdated) { |
|
24
|
|
|
expect(mowerUpdated.position).to.eql({ x:4, y:1, c:'E' }); |
|
25
|
|
|
done(); |
|
26
|
|
|
}); |
|
27
|
|
|
}); |
|
28
|
|
|
it('should stop at positon 5 4 N', function (done) { |
|
29
|
|
|
var position = new Position({x:5,y:2}, 'S'), |
|
30
|
|
|
mower = new Mower(0, grid, position, ['A','G','A','G','A','A','D','A','G','A']); |
|
31
|
|
|
mower.start().then(function(mowerUpdated) { |
|
32
|
|
|
expect(mowerUpdated.position).to.eql({ x:5, y:4, c:'N' }); |
|
33
|
|
|
done(); |
|
34
|
|
|
}); |
|
35
|
|
|
}); |
|
36
|
|
|
}); |
|
37
|
|
|
describe('runStep', function () { |
|
38
|
|
|
var grid = new Grid(new Position({x:9,y:9}, 'S')); |
|
39
|
|
|
it('should stop at positon 3 2 E', function (done) { |
|
40
|
|
|
var position = new Position({x:2,y:2}, 'E'); |
|
41
|
|
|
var mower = new Mower(0, grid, position, ['A']); |
|
42
|
|
|
mower.runStep(0,function(mowerUpdated) { |
|
43
|
|
|
expect(mowerUpdated.position).to.eql({ x:3, y:2, c:'E' }); |
|
44
|
|
|
done(); |
|
45
|
|
|
}); |
|
46
|
|
|
}); |
|
47
|
|
|
it('should stop at positon 1 2 W', function (done) { |
|
48
|
|
|
var position = new Position({x:2,y:2}, 'W'), |
|
49
|
|
|
mower = new Mower(0, grid, position, ['A']); |
|
50
|
|
|
mower.runStep(0,function(mowerUpdated) { |
|
51
|
|
|
expect(mowerUpdated.position).to.eql({ x:1, y:2, c:'W' }); |
|
52
|
|
|
done(); |
|
53
|
|
|
}); |
|
54
|
|
|
}); |
|
55
|
|
|
it('should stop at positon 2 3 N', function (done) { |
|
56
|
|
|
var position = new Position({x:2,y:2}, 'N'), |
|
57
|
|
|
mower = new Mower(0, grid, position, ['A']); |
|
58
|
|
|
mower.runStep(0,function(mowerUpdated) { |
|
59
|
|
|
expect(mowerUpdated.position).to.eql({ x:2, y:3, c:'N' }); |
|
60
|
|
|
done(); |
|
61
|
|
|
}); |
|
62
|
|
|
}); |
|
63
|
|
|
it('should stop at positon 2 1 S', function (done) { |
|
64
|
|
|
var position = new Position({x:2,y:2}, 'S'), |
|
65
|
|
|
mower = new Mower(0, grid, position, ['A']); |
|
66
|
|
|
mower.runStep(0,function(mowerUpdated) { |
|
67
|
|
|
expect(mowerUpdated.position).to.eql({ x:2, y:1, c:'S' }); |
|
68
|
|
|
done(); |
|
69
|
|
|
}); |
|
70
|
|
|
}); |
|
71
|
|
|
}); |
|
72
|
|
|
describe('isValid', function () { |
|
73
|
|
|
var grid = new Grid(new Position({x:8,y:8}, 'N')), |
|
74
|
|
|
position = new Position({x:3,y:3}, 'E'); |
|
75
|
|
|
it('should throw an error when ID is not valid', function () { |
|
76
|
|
|
expect(function() { |
|
77
|
|
|
new Mower(false, grid, position, ['A']); |
|
78
|
|
|
}).to.throwException(/Id is not valid/); |
|
79
|
|
|
}); |
|
80
|
|
|
it('should throw an error when GRID is not valid', function () { |
|
81
|
|
|
expect(function() { |
|
82
|
|
|
new Mower(0, false, position, ['A']); |
|
83
|
|
|
}).to.throwException(/Grid is not valid/); |
|
84
|
|
|
}); |
|
85
|
|
|
it('should throw an error when POSITION is not valid', function () { |
|
86
|
|
|
expect(function() { |
|
87
|
|
|
new Mower(0, grid, false, ['A']); |
|
88
|
|
|
}).to.throwException(/Position is not valid/); |
|
89
|
|
|
}); |
|
90
|
|
|
it('should throw an error when INSTRUCTIONS are not valid', function () { |
|
91
|
|
|
expect(function() { |
|
92
|
|
|
new Mower(0, grid, position, false); |
|
93
|
|
|
}).to.throwException(/Instructions are not valid/); |
|
94
|
|
|
}); |
|
95
|
|
|
}); |
|
96
|
|
|
}); |